# -*- coding: UTF-8 -*-
import os
import re,sys,urllib.request,time
import pandas as pd
from Tools import printinfo
from Tools import printinfos
def createCodeList(list_string,step):
    # print(len(list_string))
    total_count =int(len(list_string))
    step=int(step)

    # for_count = step / total_count
    for_count = total_count / step
    if total_count % step == 0:
        for_count = total_count / step
        # print("整除for_count:=", for_count)
    else:
        for_count = total_count / step + 1
        # print("未整除for_count:=", for_count)

    for_count = int(for_count)
    printStr ="total_count:【"+str(total_count)+"】\tstep:【"+str(step)+"】\t准备循环次数:【"+str(for_count)+"】"
    printinfo(printStr)
    # printinfo("开始循环。。。。")
    i=0
    tmp=0
    count=0
    returnList=[]
    #大循环次数 Start/////////////////////////////////////////////////////////////////////////////////////////////////////////
    for i in range(for_count):
        createList = []
        tmps="*"*30+"[循环第"+str(i+1)+"次"
        x=tmp
        y=step*(i+1)
        # print(tmps,"x=",x,"y=",y,"]","*"*30)
        tmp=y
        #小循环，获取截断的字符串
        for z in range(x,y):
            if count < total_count:
                createList.append(list_string[z])
                # print(z,list_string[z])
                count+=1
        # print(createList)
        x_createList =",".join(createList)
        returnList.append(x_createList)
        # print(x_createList)
    return returnList
    # printinfo("结束循环。。。。")
    #大循环次数 End/////////////////////////////////////////////////////////////////////////////////////////////////////////
    # if(count==total_count):
    #     printinfo("大循环次数("+str(i+1)+") 小循环次数("+str((count))+")","RED")


def create_codelist(lists,STEP):
    reture_createlist=[]
    total_count =len(lists)
    step = int(STEP)
    for_count = step / total_count
    if total_count % step == 0:
        for_count = total_count / step
    else:
        for_count = total_count / step + 1
    for_count=int(for_count)
    print("total_count("+str(total_count)+"), for_count("+str(for_count)+")")
    tmp=0
    for i in range(for_count):
        x = tmp
        y = step * (i + 1)
        # print("[i=",i,",x=",x,",y=",y,"]")
        # print(lists[x:y])
        reture_createlist.append(lists[x:y])
        # print(",".join(lists[x:y]))
        tmp = y
    return reture_createlist

def list_code():

    df = pd.read_csv("stocklist.csv", converters={'symbol': str})

     # print(df["ts_code"].str.split('.', expand=True))
    codelist = df["symbol"].values.tolist()
    # print(codelist)
    lsit_str = []
    for list in codelist:
        if list[0] == "0":
            lsit_str.append("sz" + list)
        if list[0] == "6":
            lsit_str.append("sh" + list)
        if list[0] == "8":
            lsit_str.append("bj" + list)
        if list[0] == "3":
            lsit_str.append("sz" + list)
        if list[0] == "4":
            lsit_str.append("bj" + list)
        # print(lsit_str)
    xxxTmp="文件读取长度" + str(len(codelist))+ "处理后长度"+ str(len(lsit_str))+str( type(lsit_str))
    printinfo(xxxTmp,"RED")
    return lsit_str

if __name__ == '__main__':
    STEP=800
    lists=[]
    for k in range(65,91):
        lists.append(chr(k))
    listx = createCodeList(lists,STEP)
    printinfos(True,str(listx))
    # print(",".join(listx))
    # print(type(lists[0:20]))
    print("="*100)
    # print(",".join(lists[0:8]))
    #
    # print()
    lists = list_code()
    LLLL = create_codelist(lists,STEP)
    # print(LLLL)
    for i in range(len(LLLL)):
        # print(i+1)
        printinfo(str(len(LLLL[i])), "BLUE")
        # if i % 2 ==0:
        #     # printinfo(",".join(LLLL[i]),"BLUE")
        #     printinfo(str(len(LLLL[i])), "BLUE")
        # else:
        #     printinfo(",".join(LLLL[i]), "RED")
        # print("="*200)
